package com.zhb.zhbaiagent.tools;

import cn.hutool.core.io.FileUtil;
import com.zhb.zhbaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * 文件操作类
 */
public class FileOperationTools {
    private static final String FILE_DIR = FileConstant.FILE_SAVE_DIR+"/file";

    @Tool(description = "Read content from a file")
    public String ReadFile(@ToolParam(description = "Name of the file to read") String FileName){
        String filePath = FILE_DIR+"/"+FileName;
        //提升代码健壮性
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "Error reading file: " + e.getMessage();
        }
    }

    public String WriteFile(@ToolParam(description = "Name of the file to write") String FileName,
                            @ToolParam(description = "Content to write to the file") String Content){
        String filePath = FILE_DIR+"/"+FileName;
        try {
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(Content,filePath);
            return "File written successfully to: " + filePath;
        } catch (Exception e) {
            return "Error writeing file: " + e.getMessage();
        }

    }
}

